package temp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import src.RTPpacket;


public class MediaReceiver implements Runnable {

	private MediaPacketReceiver mediaPacketReceiver;
	private DatagramSocket receiverSocket;
	private DatagramPacket currentPacket;
	
	private boolean close;
	
	
	public MediaReceiver(ConnectionDescription connectionDescription,MediaPacketReceiver mediaPacketReceiver) {
		super();
		this.close = false;
		this.mediaPacketReceiver = mediaPacketReceiver;
		
		try {
			receiverSocket = new DatagramSocket(55776);
			currentPacket = new DatagramPacket(new byte[connectionDescription.getPayLoadInBytes()],connectionDescription.getPayLoadInBytes());
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}



	public void run() {
		
		while(!close){
			try {
				receiverSocket.receive(currentPacket);
				mediaPacketReceiver.wite(new RTPpacket(currentPacket.getData(),currentPacket.getLength()));
				
			} catch (IOException e) {
				e.printStackTrace();
			}
						
		}
	}
	
	public void close(){
		close = true;
	}
}
